<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class m_user extends CI_Model{
    function __construct() {
        parent::__construct();
    }
    
    function insert_user($email,$password,$nama,$tanggal_lahir){
        $n_salt = $this->generateSalt();
        $password = md5($password.$n_salt);
        $this->db->query("INSERT INTO user (email,password,salt,nama,tanggal_lahir,is_aktif) VALUES ('$email','$password','$n_salt','$nama','$tanggal_lahir','0')");
        return $this->db->insert_id();
    }
    
    //SELECT
    function get_user($id_user){
        $this->db->where('user.id',$id_user,TRUE);
        $query = $this->db->get('user');
        if($query->num_rows()>0){
            return $query->first_row();
        }else{
            return null;
        }
    }
    
    function get_userid($email){
          $data_user = $this->db->query("SELECT * FROM user WHERE email ='$email'");
          return $data_user;   
    }


    function generateSalt(){
        $charList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $i = 0;
        $salt = "";
        do{
            $salt .= $charList{mt_rand(0, strlen($charList))};
            $i++;
        }while($i<15);
        return $salt;
    }
    
}